<template>
  <!-- 新增合同类型 -->
  <a-modal
    title="新增合同类型"
    :visible="visible"
    :confirm-loading="confirmLoading"
    @ok="confirmAddType"
    @cancel="$emit('update:visible', false)"
  >
    <div>
      <a-form :form="typeForm" :label-col="{ span: 7 }" :wrapper-col="{ span: 16 }" :colon="true">
        <a-form-item label="合同模板名称">
          <a-input
            style="width: 300px"
            placeholder="请输入合同模板名称"
            v-decorator="['name', { rules: [{ required: true, message: '请输入' }] }]"
          />
        </a-form-item>
        <a-form-item label="分组">
          <a-select
            style="width: 300px"
            placeholder="请选择分组"
            v-decorator="['groupId', { rules: [{ required: true, message: '请选择' }] }]"
          >
            <a-select-option v-for="(group, index) in groupList" :key="index" :value="group.id">{{ group.name }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="名称描述">
          <a-textarea
            style="width: 300px"
            v-decorator="['description', { rules: [{ required: false, message: '请输入' }] }]"
            placeholder="请输入名称描述"
            :rows="4"
          />
        </a-form-item>
      </a-form>
    </div>
  </a-modal>
</template>

<script>
import { getContractGroups, addCategories, templateCreateReq } from "@/api/company/contract";
import { isContainByString } from "@/utils/index";
export default {
  props: {
    isLoadingGroup: {
      type: Boolean,
      default: false
    },
    visible: {
      type: Boolean,
      default: false
    },
    id: {
      type: String,
      default: ""
    }
  },
  watch: {
    visible(v) {
      if (v && !this.isLoadingGroup) {
        this.onGetContractGroups();
        this.typeForm.resetFields();
      }
    }
  },
  data() {
    return {
      isContainByString,
      confirmLoading: false,
      groupList: [],
      typeForm: this.$form.createForm(this)
    };
  },
  created() {},
  methods: {
    // 获取分组列表
    async onGetContractGroups() {
      try {
        const { data } = await getContractGroups({
          subjectId: this.isContainByString("contractAdmin") ? "0" : this.$store.state.hr.companyId
        });
        for (var i = 0; i < data.length; i++) {
          data[i].index = i;
        }
        this.groupList = data;
      } catch (err) {
        console.log("error log --> ", err);
      }
    },

    // 确定增加合同分类
    confirmAddType(e) {
      this.typeForm.validateFields((err, values) => {
        values.sort = "1";
        values.subjectId = this.isContainByString("contractAdmin") ? "0" : this.$store.state.hr.companyId;
        if (!err) {
          this.confirmLoading = true;
          this.postAddCategories(values);
        }
      });
    },
    async postAddCategories(values) {
      try {
        const { data } = await addCategories(values);
        this.$message.success("添加成功");
        this.$emit("update:visible", false);
        this.confirmLoading = false;
        sessionStorage.setItem("contractTypeQueryName", data.name);
        sessionStorage.setItem("editSealNewStatus", 0);
        sessionStorage.setItem("categoryUpdateDTO", "{}");
        sessionStorage.setItem("signedsStatus", "");
        this.$router.push({
          name: this.isContainByString("contractAdmin") ? "settingAdmin" : "setting",
          query: {
            id: data.id,
            groupId: data.groupId
          }
        });
      } catch (err) {
        this.confirmLoading = false;
        console.log("error log --> ", err);
      }
    },
    async templateCreateReq(name, categoryId) {
      try {
        await templateCreateReq({
          categoryId,
          name
        });
      } catch (err) {
        console.log("error log --> ", err);
      }
    }
  }
};
</script>
<style lang="scss"></style>
